﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using AutoMapper;
using good1.Core;
using good1.Core.Enums;
using good1.Web.Controllers.Utility;
using good1.Web.Model;
using good1.ApplicationServices.Interface;
using good1.Web.Controllers.Attributes;
using SharpArch.Core;
using good1.Web.Helpers;

namespace good1.Web.Controllers
{
    public class SignUpController : BaseController
    {

        public SignUpController(IAuthenticationService authenticationService, ISecurityContextService securityContext) : base(securityContext)
        {
            Check.Require(authenticationService != null, "authenticationService may not be null");

            this.authenticationService = authenticationService;

        }

        public ActionResult Index(string m)
        {
            SignUpForm form = new SignUpForm { EntreatyMessage = GetEntreatyMessage(m), Genders = Gender.Female.ToSelectListWithoutNone() };
            form = SetViewData(form);
            return View(form);
        }

        private static string GetEntreatyMessage(string message)
        {
            switch (message)
            {
                case "coming_soon":
                    return
                        "We are working on getting things running," +
                        " in the meantime please sign up below and we'll be in touch";
                   
                default:
                    return "";
            }
        }

        public ActionResult ThankYou()
        {
            ThankYouView thankYouFormView = CreateViewData<ThankYouView>();
            return View(thankYouFormView);
        }

        public ActionResult VerifyEmailAddress(string key)
        {
            VerifiedEmailView verifiedEmailView = CreateViewData<VerifiedEmailView>();
            User user = authenticationService.ApproveEmailAddress(new Guid(key));
            FormsAuthentication.SetAuthCookie(user.Username, false);
            return View(verifiedEmailView);
            //TODO write thanks username and some blurb
        }

        [CaptchaValidator]  
        [AcceptVerbs( HttpVerbs.Post )]
        public ActionResult Index(SignUpForm signUpForm, bool captchaValid, FormCollection formCollection)  
        {
            if (ValidateRegistration(signUpForm, captchaValid))
            {
                Mapper.CreateMap<int, Gender>().ConvertUsing(new MapperHelper.IntEnumConverter<Gender>());
                Mapper.CreateMap<SignUpForm, User>();
                User user = Mapper.Map<SignUpForm, User>(signUpForm);
                //TODO nice to have a signupform with password and then specific mapper to user not auto.
                authenticationService.Register(user, signUpForm.WantsMonthlyNewsUpdates, signUpForm.Password, null, null);
                return RedirectToAction<SignUpController>(l => l.ThankYou());            
            }
            return View(signUpForm);//failed registration
        }

        private bool ValidateRegistration(SignUpForm signUpForm, bool captchaValid)
        {
            if(!captchaValid)
            {
                ModelState.AddModelError("captcha", "Please try the two words again, if they are difficult to read you can click for different words above.");
            }

            if (string.IsNullOrEmpty(signUpForm.EmailAddress))
            {
                ModelState.AddModelError("EmailAddress", "We must have an email address");
            }

            if (!authenticationService.CheckAvailability(signUpForm.EmailAddress))
            {
                ModelState.AddModelError("EmailAddress", "We already have your email on file");                
            }

            if (signUpForm.Password!=signUpForm.ConfirmPassword)
            {
                ModelState.AddModelError("EmailAddress", "Please re-enter, they didn't match last time.");
            }
            
            return ModelState.IsValid;
            
        }

        private readonly IAuthenticationService authenticationService;

    }
}
